//
//  PlayerSprite.m
//  voyage
//
//  Created by Stéphane BONIFFACY on 29/01/13.
//  Copyright (c) 2013 Steph. All rights reserved.
//


#import "PlayerSprite.h"

@interface PlayerSprite ()
{
    int  _selectedOption;
}

-(void)addFireOption:(id<WeaponSpriteProtocol>)weapon selectIt:(BOOL)selected;

@end

@implementation PlayerSprite

@synthesize  angle = _angle;
@synthesize  impulsion = _impulsion;
@synthesize  fireOptions = _fireOptions;

-(id)init{
    
    if((self = [super initWithFile:@"cannon.png"])){
        _angle = M_PI_2;
        _impulsion = M_PI_4; // PI/4 par seconde
        _selectedOption = -1;
        _fireOptions = [[NSMutableArray alloc] init];
    }
    
    return self;
}

-(id)initWithPosition:(CGPoint)position {
    
    if(([self init])){
        self.position = position;
    }
    
    return self;
}

-(id)initWithPosition:(CGPoint)position angle:(float)angle {
    
    if(([self init])){
        
        _angle = angle;
        self.position = position;
    }
    
    return self;
}

-(void)addFireOptionAndSelectIt:(id <WeaponSpriteProtocol>)weapon {
    [self addFireOption:weapon selectIt:NO];
}

-(void)addFireOption:(id <WeaponSpriteProtocol>)weapon {
    [self addFireOption:weapon selectIt:YES];
}

-(void)addFireOption:(id <WeaponSpriteProtocol>)weapon selectIt:(BOOL)selected{
    [_fireOptions addObject:weapon];
    
    if(selected || _selectedOption == -1) {
        _selectedOption = [_fireOptions indexOfObject:weapon];
    }
}

-(NSArray*)getFireOptions{
    
    NSMutableArray* ret = [[NSMutableArray alloc] initWithCapacity:_fireOptions.count];
    
    for (id<WeaponSpriteProtocol> weapon in _fireOptions) {
        NSDictionary* dict = [weapon getResumeInfo];
        [ret addObject:dict];
        [dict release];
    }
    
    return ret;
}

-(void)selectFireOptionIndex:(int)index {
    _selectedOption = index;
}

-(void)resetFireOption {
    _selectedOption = -1;
    [_fireOptions removeAllObjects];
}

-(id<WeaponSpriteProtocol>)getSelectedOption {
    if(_selectedOption < 0 || _selectedOption >= _fireOptions.count ) return nil;
    return [_fireOptions objectAtIndex:_selectedOption];
}

-(void)dealloc {
    [_fireOptions release];
    [super dealloc];
}

@end